<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col :span="6"><el-button type="primary" @click="addCategoryFn">添加分类</el-button></el-col>
      </el-row>

      <!--TODO表格区域 -->
      <tree-table class="el-card" :data="cateList" :columns="columns" border :show-row-hover="false" :selection-type="false" :expand-type="false" index-text="#" show-index>
        <!-- 使用自定义列 -->
        <!-- 是否有效列 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" :style="{ color: 'lightgreen' }" v-if="scope.row.cat_deleted == false"></i>
          <i class="el-icon-error" :style="{ color: 'red' }" v-else></i>
        </template>

        <!-- 排序列 -->
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level == 1">二级</el-tag>
          <el-tag type="warning" v-else>三级</el-tag>
        </template>

        <!-- 操作列 -->
        <template slot="opt">
          <el-button size="mini" icon="el-icon-edit" type="primary">编辑</el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger">删除</el-button>
        </template>
      </tree-table>
     
      <!--TODO分页区域 -->

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="params.pagenum" :page-sizes="[3, 5, 10, 15]" :page-size="params.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
   
   
    </el-card>

    <!-- 添加分类弹框 -->
    <el-dialog @close="clearFormInfo" title="提示" :visible.sync="categorydialogVisible" width="50%">

      <el-form :model="categoryFormInfo" :rules="categoryFormInforules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="分类名称" prop="cat_name"> <el-input v-model="categoryFormInfo.cat_name"></el-input> </el-form-item>

        <el-form-item label="父级分类">
          <!-- 级联选择器 -->
          <el-cascader style="width: 100%" change-on-select clearable 
          v-model="setcategoryid" :options="parentCategoryList" 
          :props="{ expandTrigger: 'hover', value: 'cat_id', label: 'cat_name', children: 'children' }">
          </el-cascader>
        
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="categorydialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoriesList, addcategoryInfo } from '@/api/index.js'
export default {
  created() {
    //* 获取商品分类数据
    this.getCategoriesFn()
  },
  data() {
    return {
      //*级联选择器选中的返回值
      setcategoryid: [],
      //*添加分类验证规则
      categoryFormInforules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }],
      },
      //*添加分类信息
      categoryFormInfo: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0,
      },
      //*添加分类弹框
      categorydialogVisible: false,
      /* 获取商品分类列表参数 */
      params: {
        type: [3],
        pagenum: 1,
        pagesize: 5,
      },
      /* 商品分类列表 */
      cateList: [],
      /* 总数据条数 */
      total: 0,

      columns: [
        {
          /* 分类名称 */
          label: '分类名称',
          //*要显示的字段名
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok',
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort',
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ],
      //*父级分类列表
      parentCategoryList: [],
    }
  },
  methods: {
    //*重置表单，初始化数据
    clearFormInfo() {
      //*重置表单数据
      this.$refs.ruleForm.resetFields()
      //*重置级联选择器绑定数据
      this.setcategoryid = []
    },
    //*添加分类
    addCategory() {
      this.$refs.ruleForm.validate(async (valid) => {
        //*表单验证
        if (valid) {
          //*如果选中了二级菜单，设置提交数据为 二级菜单
          if (this.setcategoryid[1]) {
            this.categoryFormInfo.cat_pid = this.setcategoryid[1]
            this.categoryFormInfo.cat_level = 2
          } else if (this.setcategoryid[0]) {
            this.categoryFormInfo.cat_pid = this.setcategoryid[0]
            this.categoryFormInfo.cat_level = 1
          } else {
            this.categoryFormInfo.cat_pid = 0
            this.cat_level = 0
          }
          let { cat_pid, cat_name, cat_level } = this.categoryFormInfo
          let { data: res } = await addcategoryInfo({ cat_pid, cat_name, cat_level })
          if (res.meta.status != 201) {
            return this.$message.error('添加分类失败')
          }
          //* 数据回显//*提示信息//*关闭弹窗
          this.getCategoriesFn()
          this.$message.success('添加分类成功')
          this.categorydialogVisible = false
        } else {
          this.$message.error('请输入分类名称')
        }
      })
    },
    //*获取父级分类列表
    async getParentCategoryList() {
      let { data: res } = await getCategoriesList({ type: 2 })
      if (res.meta.status != 200) {
        return this.$message.error('获取父级列表失败')
      }
      this.parentCategoryList = res.data
    },
    //*打开添加分类弹框
    addCategoryFn() {
      this.getParentCategoryList()
      this.categorydialogVisible = true
    },

    //*当前页改变
    handleCurrentChange(pagenum) {
      //*更改data中的当前页，发送请求获取数据
      this.params.pagenum = pagenum
      this.getCategoriesFn()
    },
    //*当前页数据条数改变
    handleSizeChange(pagesize) {
      //*更改data中当前数据条数，发送请求获取数据
      this.params.pagesize = pagesize
      this.getCategoriesFn()
    },

    //*获取商品分类数据
    async getCategoriesFn() {
      let { data: res } = await getCategoriesList(this.params)
      if (res.meta.status != 200) {
        return this.$message.error('获取数据失败')
      }
      console.log(res)
      this.cateList = res.data.result
      this.total = res.data.total
    },
  },
}
</script>
<style lang="less" scoped>
.el-card {
  margin-top: 12px;
}
</style>
